package com.javakc.pms.order.dispordrls.controller;

import com.javakc.commonutils.api.APICODE;
import com.javakc.pms.order.dispordrls.client.MesClient;
import com.javakc.pms.order.dispordrls.entity.DispOrdRls;
import com.javakc.pms.order.dispordrls.service.DispOrdRlsService;
import com.javakc.servicebase.handler.HctfException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ：史根根根根根键
 * @date ：2021/12/14
 * @description：
 * @modified By：
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/pms/dispordrls")
@Api(tags = "集团调度指令")
public class DispOrdRlsController {
    @Autowired
    private DispOrdRlsService dispOrdRlsService;

    @Autowired
    private MesClient mesClient;

    @PostMapping("{pageNo}/{pageSize}")
    @ApiOperation("集团调度指令带条件分页查询")
    public Page<DispOrdRls> queryByPage(@PathVariable int pageNo, @PathVariable int pageSize){
        return dispOrdRlsService.queryByPage(pageNo,pageSize);
    }

    @GetMapping("{id}")
    @ApiOperation("带id查询区域")
    public DispOrdRls queryById(@PathVariable int id ) {
        return dispOrdRlsService.getById(id);
    }

    @ApiOperation("修改集团指令下达状态，并下达指令")
    @PostMapping("updateRelease/{id}")
    public void updateRelease(@PathVariable int id ){
        //修改之前先进行查询
        DispOrdRls rls = dispOrdRlsService.getById(id);
        //查询完进行修改 状态+时间的修改
        rls.setReleaseType(1);
        rls.setReleaseTime(new Date());
        // 下达 调用生产执行指令保存方法
        APICODE apicode = mesClient.save(rls);
        if (apicode.getCode() == 20001){
            throw new HctfException(apicode.getCode(),apicode.getMessage());
        }
        //下达成功后再进行保存
        dispOrdRlsService.saveOrUpdate(rls);

    }

}
